﻿using GalaSoft.MvvmLight.Threading;
using JiveMessenger.Converters;
using JiveMessenger.Models;
using JiveMessenger.Utilities;
using JiveMessenger.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Display;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace JiveMessenger.CustomControls
{
    public sealed partial class ContactListControl : UserControl, INotifyPropertyChanged
    {
        private double _contactControlWidth;
        public double ContactControlWidth
        {
            get { return _contactControlWidth; }
            set
            {
                if (_contactControlWidth != value)
                {
                    _contactControlWidth = value;
                    RaisePropertyChanged("ContactControlWidth");
                }
            }
        }

        private int _numberOfColumns;
        public int NumberOfColumns
        {
            get { return _numberOfColumns; }
            set
            {
                if (_numberOfColumns != value)
                {
                    _numberOfColumns = value;
                    RaisePropertyChanged("NumberOfColumns");
                }
            }
        }


        public ContactListControl()
        {
            this.InitializeComponent();
            this.DataContextPlaceholder.DataContext = this;
            NumberOfColumns = 1;
            if (DesignMode.DesignModeEnabled)
            {
                ContactControlWidth = this.Width;
                return;
            }
            this.ContactList.Background = new SolidColorBrush(Colors.Transparent);
            ContactControlWidth = this.ContactList.ActualWidth;
            SetNumberOfColumns(DisplayInformation.GetForCurrentView().CurrentOrientation);
        }
        protected void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        private void OrientationChanged(DisplayInformation sender, object args)
        {

            SetNumberOfColumns(sender.CurrentOrientation);
        }
        private void SetNumberOfColumns(DisplayOrientations orientation)
        {
            ContactListItemWidthConverter conv = (ContactListItemWidthConverter)this.Resources["ContactListItemWidthConverter"];
            if (orientation == DisplayOrientations.Portrait || orientation == DisplayOrientations.PortraitFlipped)
            {
                conv.NumberOfColumns = NumberOfColumns = 1;

                this.ContactList.ItemTemplate = (DataTemplate)Resources["ContactListPortraitTemplate"];
            }
            else
            {
                if (StaticMethods.GetScreenSizeInInches() >= 5.5)
                    conv.NumberOfColumns = NumberOfColumns = 3;
                else
                    conv.NumberOfColumns = NumberOfColumns = 2;

                this.ContactList.ItemTemplate = (DataTemplate)Resources["ContactListLandscapeTemplate"];
            }
        }
        private void MenuPathGrid_Tapped(object sender, TappedRoutedEventArgs e)
        {
            e.Handled = true;
            Grid selectedItem = ((FrameworkElement)sender).GetAncestorsOfType<Grid>().FirstOrDefault();
            FlyoutBase flyoutBase = FlyoutBase.GetAttachedFlyout(selectedItem);

            flyoutBase.ShowAt(selectedItem);

        }
        private void GroupGrid_Tapped(object sender, TappedRoutedEventArgs e)
        {
            e.Handled = true;
            Grid selectedItem = sender as Grid;
            VisualGroup visualGroup = selectedItem.DataContext as VisualGroup;

            ViewModelContactList vmContactList = this.DataContext as ViewModelContactList;
            vmContactList.OpenGroupCommand.Execute(visualGroup.Group);
        }
        private void FrameworkElement_Holding(object sender, HoldingRoutedEventArgs e)
        {
            e.Handled = true;
            FrameworkElement selectedItem = sender as FrameworkElement;
            Flyout.GetAttachedFlyout(selectedItem).ShowAt(selectedItem);
        }
        private void ContactGrid_Tapped(object sender, TappedRoutedEventArgs e)
        {
            e.Handled = true;
            Grid selectedItem = sender as Grid;
            IContact contact = selectedItem.DataContext as IContact;

            ViewModelContactList vmContactList = this.DataContext as ViewModelContactList;
            vmContactList.OpenPmCommand.Execute(contact);
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            DisplayInformation.GetForCurrentView().OrientationChanged += OrientationChanged;
        }
        private void UserControl_Unloaded(object sender, RoutedEventArgs e)
        {
            DisplayInformation.GetForCurrentView().OrientationChanged -= OrientationChanged;
        }

        private void ContactList_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            //if (ContactControlWidth == 0)
            ContactControlWidth = e.NewSize.Width;

        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
#if WINDOWS_PHONE_APP
            FrameworkElement element = (FrameworkElement)sender;
            Pivot.SetSlideInAnimationGroup(element, PivotSlideInAnimationGroup.Default);
            CommonNavigationTransitionInfo.SetIsStaggerElement(element, true);
#endif
        }
        private void NamePanel_Loaded(object sender, RoutedEventArgs e)
        {
#if WINDOWS_PHONE_APP
            FrameworkElement element = (FrameworkElement)sender;
            Pivot.SetSlideInAnimationGroup(element, PivotSlideInAnimationGroup.GroupOne);
            CommonNavigationTransitionInfo.SetIsStaggerElement(element, true);
#endif
        }

    }
}
